var express = require('express'); 
var app = express(); 
  
// Creates a server which runs on port 3000 and  
// can be accessed through localhost:3000 
app.listen(3000, function() { 
    console.log('server running on port 3000'); 
} ) 
app.listen(3001)

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
    res.header("x-Trigger", "CORS");
    next();
  });
  
// Function callName() is executed whenever  
// url is of the form localhost:3000/name 
app.get('/name', callName); 

const RequestIp = require('@supercharge/request-ip')

function callName(req, res) { 
    var ip = RequestIp.getClientIp(req)
    // Use child_process.spawn method from  
    // child_process module and assign it 
    // to variable spawn 
    var spawn = require("child_process").spawn; 
      
    // Parameters passed in spawn - 
    // 1. type_of_script 
    // 2. list containing Path of the script 
    //    and arguments for the script  
    
    console.log(req.query.firstname)
    // var ip = req.headers['x-real-ip']|| req.connection.remoteAddress
    console.log(ip)
    // E.g : http://localhost:3000/name?firstname=Mike&lastname=Will 
    // so, first name = Mike and last name = Will 
    var process = spawn('D:\\Python\\python.exe',["./hello.py", 
                            req.query.firstname, 
                            req.query.lastname] ); 
  
    // Takes stdout data from script which executed 
    // with arguments and send this data to res object 
    process.stdout.on('data', function(data) { 
        res.send(data.toString()); 
    } ) 
} 
  
// save code as start.js 